package com.banciyuan.bcywebview.biz.circles;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.CoreApi;
import com.banciyuan.bcywebview.api.TagApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hy on 2016/4/20.
 */
public class CircleLikeHelper {


    public static void doCircleLike(final Context context, String id, String circleType, String followType, final CircleLikeSuccess circleLikeSuccess) {

        RequestQueue mQueue = VolleyQueue.getRquest(context);
        Map<String, String> map;
        List<BcyPair> paramsList = new ArrayList<>();
        String url = HttpUtils.BASE_URL + CoreApi.getFollow();
        if ("work".equals(circleType)) {
            paramsList.add(new BcyPair(HttpUtils.WID, id));
        } else if ("tag".equals(circleType)) {
            paramsList.add(new BcyPair(HttpUtils.TAG_ID, id));
            url = HttpUtils.BASE_URL + TagApi.getFollow();
        }
        paramsList.add(new BcyPair(HttpUtils.TYPE, followType));
        if (UserDataHelper.ifLogin(context)) {
            paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(context).getToken()));
        } else {
            MyToast.show(context, context.getString(R.string.loginfirst));
            return;
        }
        map = HttpUtils.getData(paramsList);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String result) {
                if (StatusCodeUtils.dealStatusCode(result, context)) {
                    if (null != circleLikeSuccess)
                        circleLikeSuccess.onCircleLikeSuccess();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
            }
        };
        StringRequestParam getUserdataRequest = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(getUserdataRequest);
    }

    public interface CircleLikeSuccess {
        void onCircleLikeSuccess();
    }
}
